package main

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/robfig/cron"
	"influxdbDemoData/influxdb"
	"influxdbDemoData/mysql"
	"influxdbDemoData/sysinit"
	"influxdbDemoData/util"
	"log"
	"strconv"
	"time"
)

var influxdbConnectK11 *influxdb.Connect
var timeExecK11 TimeExecK11

type TimeExecK11 struct {
	Status    string
	StartTime string
	EndTime   string
	Cron      string
}

func main() {
	log.Println("====== main start ======")
	ch := make(chan int, 1)
	initK11()
	RunFuncK11()
	if timeExecK11.Status == "true" {
		for _, date := range util.ComputeTime(timeExecK11.StartTime, timeExecK11.EndTime) {
			execK11(date)
		}
	}
	for {
		<-ch
	}
	log.Println("====== main exit ======")
}
func RunFuncK11() {
	c := cron.New()
	c.AddFunc(timeExecK11.Cron, func() {
		today := time.Now().Format(util.LayoutDate)
		log.Println(fmt.Sprintf("today %v", today))
		execK11(today)
	})
	c.Start()
	log.Println(fmt.Sprintf("cron %+v", timeExecK11.Cron))
}

func execK11(date string) {

	flow(date)
	flowBusiness(date)

}

func flow(date string) {
	pf, err := mysql.SelectPassengerflowByDate(date)
	if err != nil {
		panic(err)
	}

	var flows []influxdb.Flow

	for _, value := range *pf {
		flow := influxdb.Flow{
			Time:       value.Time,
			ZoneId:     strconv.Itoa(value.ZoneId),
			CountEnter: value.CountEnter,
			CountExit:  value.CountExit,
		}
		flows = append(flows, flow)
	}
	log.Println(fmt.Sprintf("date:%v find: %v", date, len(*pf)))
	log.Println(fmt.Sprintf("-------- flows: %v", len(flows)))
	//log.Println(fmt.Sprintf("-------- flows: %+v", flows))
	insert := influxdbConnectK11.InsertPassengerflow(&flows, influxdb.Passengerflow)
	log.Println(fmt.Sprintf("-------- insert flows: %v", insert))
}
func flowBusiness(date string) {
	pfb, err := mysql.SelectPack5ByDate(date)
	if err != nil {
		panic(err)
	}
	var flowsBusiness []influxdb.Flow

	for _, value := range *pfb {
		date, err := time.ParseInLocation(util.LayoutDate, value.Date, time.Local)
		if err != nil {
			panic(err)
		}
		time := time.Date(date.Year(), date.Month(), date.Day(), value.DateHour, value.DateMin, 0, 0, time.Local)
		flow := influxdb.Flow{
			Time:       time,
			ZoneId:     strconv.Itoa(value.ZoneId),
			CountEnter: value.CountEnter,
			CountExit:  value.CountExit,
		}
		flowsBusiness = append(flowsBusiness, flow)
	}

	log.Println(fmt.Sprintf("date:%v find: %v", date, len(*pfb)))
	log.Println(fmt.Sprintf("-------- flowsBusiness: %v", len(flowsBusiness)))
	//log.Println(fmt.Sprintf("-------- flowsBusiness: %+v", flowsBusiness))
	insert := influxdbConnectK11.InsertPassengerflow(&flowsBusiness, influxdb.PassengerflowBusiensss)
	log.Println(fmt.Sprintf("-------- insert flowsBusiness: %v", insert))
}

func initK11() {
	beego.LoadAppConfig("ini", "conf/app-k11.conf")
	sysinit.InitDatabase([]string{"iretailer"})
	sysinit.InitLogs()
	//创建链接Influxdb
	iaddr := beego.AppConfig.String("influxdb::addr")
	ipassword := beego.AppConfig.String("influxdb::password")
	iusername := beego.AppConfig.String("influxdb::username")
	idbname := beego.AppConfig.String("influxdb::dbname")
	itimezone := beego.AppConfig.String("influxdb::timezone")

	influxdbConnectK11 = influxdb.NewConnect(iaddr, iusername, ipassword, idbname, itimezone)

	//TimeInterval
	timeExecK11.Status = beego.AppConfig.String("timeInterval::status")
	timeExecK11.StartTime = beego.AppConfig.String("timeInterval::start_time")
	timeExecK11.EndTime = beego.AppConfig.String("timeInterval::end_time")
	timeExecK11.Cron = beego.AppConfig.String("timeInterval::cron")
}
